package tue.cse.advanceddatabases.project.xml.artist;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class Artist {

	private Element element;

	public Artist() {
		this.element = DocumentHelper.createElement("artist");
	}

	public Artist(Element element) {
		this.element = element;
	}

	public String getId() {
		return element.attributeValue("id");
	}

	public void setId(String value) {
		if (element.attribute("id") == null)
			element.addAttribute("id", value);
		else
			element.attribute("id").setValue(value);

	}

	public String getName() {
		return element.elementText("name");
	}

	public void setName(String value) {
		if (element.element("name") == null)
			element.addElement("name").setText(value);
		else
			element.element("name").setText(value);
	}

	public String getDescription() {
		return element.elementText("description");
	}

	public void setDescription(String value) {
		if (element.element("description") == null)
			element.addElement("description").setText(value);
		else
			element.element("description").setText(value);

	}

	public String getImage() {
		return element.elementText("image");
	}

	public void setImage(String value) {
		if (element.element("image") == null)
			element.addElement("image").setText(value);
		else
			element.element("image").setText(value);
	}

	public String getUrl() {
		return element.elementText("url");
	}

	public void setUrl(String value) {
		if (element.element("url") == null)
			element.addElement("url").setText(value);
		else
			element.element("url").setText(value);
	}

	public String getDate_created() {
		return element.elementText("data_created");
	}

	public void setDate_created(String value) {
		if (element.element("data_created") == null)
			element.addElement("data_created").setText(value);
		else
			element.element("data_created").setText(value);
	}
	
	public String getGenre(){
	  	return element.elementText("genre");
	}
	public void setGenre(String value){
    	  	if (element.element("genre") == null)
    	  	  	element.addElement("genre").setText(value);
    	  	else
    	  	  	element.element("genre").setText(value);
	}
	
	public String getRank() {
	  	//GORAN: TODO: check. it should be rating, not tank
		//return element.elementText("rank");
	  	return element.elementText("rating");
	}

	public void setRank(String value) {
	  	
	}

	public String getRating() {

		if ( (element.elementText("rating") == null) || 
			 (element.elementText("rating-count")==null) ||
			 (element.elementText("rating").length() <= 0) || 
			 (element.elementText("rating-count").length() <=0 ) ||
			  "0".equalsIgnoreCase(element.elementText("rating")) ||
			  "0".equalsIgnoreCase(element.elementText("rating-count"))) {
			return "no";
		}

		long rating = Long.valueOf(element.elementText("rating")).longValue();
		long ratingCount = Long.valueOf(element.elementText("rating-count"))
				.longValue();

		long rateValue = (long) Math.ceil(rating / ratingCount);

		return String.valueOf(rateValue);

	}

	public String getCommentCount(){
		return element.elementText("comment-count");
	}
	
	public void setCommentCount(String value) {
		// do nothing. it is automatic done when commenting.
	}	
}
